<?php
class Core_Video_YouTube{
	const VENDOR_YOUTUBE		= 'YouTube';
	
	const THUMB_TYPE_DEFAULT	= 'default';
	const THUMB_TYPE_BIGSIZE	= '0';
	
	protected $_name;
	protected $_src;
	protected $_width = 800;
	protected $_height = 450;
	protected $_frameBorder		= 0;
	protected $_allowFullScreen	= TRUE;
	
	public function __construct(array $option = null)
	{
		if( isset($option['src']) )
		{
			$this->_src		= $option['src'];
		}
		if( isset($option['width']) )
		{
			$this->_width	= $width;
		}		
		if( isset($option['height']) )
		{
			$this->_height	= $height;
		}
	}
	
	public function getAllowFullScreen()
	{
		return $this->_allowFullScreen;
	}
	public function setAllowFullScreen($value = true)
	{
		$this->_allowFullScreen = $value;
		return $this;
	}
	public function getFrameBorder()
	{
		return $this->_frameBorder;
	}
	public function setFrameBorder($value = 0)
	{
		$this->_frameBorder = $value;
		return $this;
	}


	/**
	 * @author hungnq@younetco.com
	 * @param int $width
	 * @param int $height
	 * @return string embeded code 
	 */
	public function getEmbededdCode($width = null, $height = null)
	{
		$embeded	= "http://www.youtube.com/embed/%s?wmode=transparent";
		$src		= sprintf($embeded, $this->getVideoCode());
		$properties = array(
			'width'				=> isset($width)?$width:$this->_width,
			'height'			=> isset($height)?$height:$this->_height,
			'src'				=> $src,
			'frameborder'		=> $this->_frameBorder,
			'allowfullscreen'	=> $this->_allowFullScreen
		);

		$startTag = "<iframe id='iframe-video'";
		
		foreach($properties as $key => $value)
		{
			$format = " %s=\"%s\"";
			$startTag .= sprintf($format, $key, $value);
		}
		$startTag .= ">";
		
		$endTag = "</iframe>";
		
		return sprintf("%s%s", $startTag, $endTag);
	}
	
	/**
	 * e.g http://www.youtube.com/watch?v=embeded_code
	 * get embeded_code in the url above
	 * @author hungnq@younetco.com
	 * @return string $video_code
	 */
	protected function getVideoCode()
	{
		$value = '';
		try{
			$url	= $this->_src;
			$purl	= parse_url($url);
			if( !empty($purl['query']) )
			{
				parse_str($purl['query'], $video_code);
				if( !empty($video_code['v']) )
				{
					$value = $video_code['v'];
				}
			}
			else
			{
				if( !empty($purl['path']) )
				{
					$value = substr($purl['path'],1);
				}
			}
		} catch(Exception $e) {

		}
		return $value;
	}
	
	/**
	 * @author hungnq@younetco.com
	 * @param type $thump_type
	 * @return type 
	 */
	public function getThumbnailUrl()
	{
		$video_code = $this->getVideoCode();
		$format		= "http://img.youtube.com/vi/%s/%s.jpg";
		return sprintf($format, $video_code, self::THUMB_TYPE_DEFAULT);
	}
	
	public function getImageUrl()
	{
		$video_code = $this->getVideoCode();
		$format		= "http://img.youtube.com/vi/%s/%s.jpg";
		return sprintf($format, $video_code, self::THUMB_TYPE_BIGSIZE);
	}
	
	final public function getName()
	{
		return 'YouTube';
	}
	
	/**
	 *
	 * @return boolean 
	 */
	public function isExist() {
		$youtubeId	= $this->getVideoCode();
		$headers	= get_headers('http://gdata.youtube.com/feeds/api/videos/' . $youtubeId);
		if (!strpos($headers[0], '200')) {			
			return false;
		}
		return true;
	}
}